#ifndef _FLV_PARSE_H_
#define _FLV_PARSE_H_

#include <iostream>
#include <queue>
#include <string> 
#include <stdint.h>

// 9 + 4 pre_tag_size (0)
#define FLV_HEADER_LEN (13)
#define TAG_LEN (11)
#define PRE_TAG_LEN (4)
#define SrsAudioCodecIdAAC  (10)
#define SrsVideoCodecIdAVC  (7)
#define VIDEO_TAG_HEADER  (5)
#define SequenceHeader  (0)

const uint8_t nal_start_code[4] = {0x00,0x00,0x00,0x01};

static inline uint32_t  Read24(uint8_t *pBuf)
{
    return (pBuf[0] << 16) | (pBuf[1] << 8) | (pBuf[2]);
}

static inline uint32_t Read16(uint8_t *pBuf)
{
    return (pBuf[0] << 8) | (pBuf[1]);
}

static inline uint8_t Read8(uint8_t *pBuf)
{
    return (pBuf[0]);
}

struct TAG {
public:
      TAG(){
           tag_data = nullptr;
           type = 0;
           timestamp = data_size = streamid = pre_tag_size = 0;
      }
      ~TAG(){
          std::cout<<"free tag data"<<std::endl;
          if (tag_data != nullptr) {
             free(tag_data);
          }
      }
      uint8_t type;
      uint32_t timestamp;
      uint32_t data_size;
      uint32_t streamid;
      uint8_t *tag_data;
      uint32_t pre_tag_size;
};

class FLV_Parser {
public:
    FLV_Parser();
    ~FLV_Parser();
    
    std::queue<TAG*> vq;
    std::queue<TAG*> aq;
    std::queue<TAG*> sq;
    int consume(char *data, int total);
private:
   // uint8_t flvheader[FLV_HEADER_LEN];
    uint8_t * unused;
    uint32_t unused_len;
    bool    header_ok;
    uint8_t mAudioProfile, mSampleRate, mChannels;
    float mFrameRate;
};

#endif
